package com.winhxd.b2c.order.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.context.CustomerUser;
import com.winhxd.b2c.common.context.UserContext;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.OrderSpuFavoriteEditCondition;
import com.winhxd.b2c.common.domain.order.condition.OrderSpuFavoriteQueryCondition;
import com.winhxd.b2c.common.domain.order.vo.OrderSpuFavoriteVO;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.order.service.OrderSpuFavoriteService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 收藏夹
 * @author suhaochan
 * @date 2019/1/7 20:34:07
 */
@RestController
@Api(tags = "ApiOrderSpuFavoriteController")
@RequestMapping(value = "/api-order/order")
public class ApiOrderSpuFavoriteController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ApiOrderSpuFavoriteController.class);
	
	@Autowired
	private OrderSpuFavoriteService orderSpuFavoriteService;

    @ApiOperation(value = "加入收藏夹", response = Long.class,notes = "加入收藏夹")
    @RequestMapping(value = "/4230/v1/addFavorite", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Long> addFavorite(@RequestBody OrderSpuFavoriteEditCondition condition){
    	String logTitle = "/api-order/order/4230/v1/addFavorite-加入收藏夹";
    	LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
    	ResponseResult<Long> result = new ResponseResult<>();
    	CustomerUser customer = UserContext.getCurrentCustomerUser();
    	if(customer == null || customer.getCustomerId() == null){
    	    LOGGER.error(logTitle+"--用户为空！");
    	    throw new BusinessException(BusinessCode.CODE_403001);
    	}
    	condition.setCustomerId(customer.getCustomerId());
    	if (condition.getSpuId() == null) {
    		throw new BusinessException(BusinessCode.CODE_403006);
    	}
    	int data = orderSpuFavoriteService.saveFavorite(condition);
    	result.setData((long) data);
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }

    @ApiOperation(value = "删除收藏夹", response = Long.class,notes = "删除收藏夹")
    @RequestMapping(value = "/4231/v1/removeFavorite", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Long> removeFavorite(@RequestBody OrderSpuFavoriteEditCondition condition){
    	String logTitle = "/api-order/order/4231/v1/removeFavorite-删除收藏夹";
    	LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
    	ResponseResult<Long> result = new ResponseResult<>();
    	CustomerUser customer = UserContext.getCurrentCustomerUser();
    	if(customer == null || customer.getCustomerId() == null){
    	    LOGGER.error(logTitle+"--用户为空！");
    	    throw new BusinessException(BusinessCode.CODE_403001);
    	}
    	condition.setCustomerId(customer.getCustomerId());
    	if (condition.getFavoriteId() == null) {
    		throw new BusinessException(BusinessCode.CODE_403006);
    	}
    	int data = orderSpuFavoriteService.removeFavorite(condition);
    	result.setData((long) data);
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }

    @ApiOperation(value = "查询收藏夹商品", response = OrderSpuFavoriteVO.class,notes = "查询收藏夹商品")
    @RequestMapping(value = "/4232/v1/findFavorite", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<PagedList<OrderSpuFavoriteVO>> findFavorite(@RequestBody OrderSpuFavoriteQueryCondition condition){
    	String logTitle = "/api-order/order/4232/v1/findFavorite-查询收藏夹商品";
    	LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
    	ResponseResult<PagedList<OrderSpuFavoriteVO>> result = new ResponseResult<>();
    	CustomerUser customer = UserContext.getCurrentCustomerUser();
    	if(customer == null || customer.getCustomerId() == null){
    	    LOGGER.error(logTitle+"--用户为空！");
    	    throw new BusinessException(BusinessCode.CODE_403001);
    	}
    	condition.setCustomerId(customer.getCustomerId());
    	PagedList<OrderSpuFavoriteVO> data = orderSpuFavoriteService.findFavorite(condition);
    	result.setData(data);
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }
}
